package com.tristia.entity.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.tristia.dao.GenericDao;
import com.tristia.entity.User;

public class UserDataLoader {
	private List<User> users = new ArrayList<User>();
	@Resource
	GenericDao<User, Long> userDao;
	
	public void loadData() throws IOException {
		
		File defaultFile = new File("src/main/webapp/imgs/img0.jpg");
		int i = 1;
		for (User user : users) {
//			File file = new File("D://Lulu14_normal.jpg");
			File file = new File("src/main/webapp/imgs/img"+i+".jpg");
			if (file.length()==0) {
				file = defaultFile;
			}
			FileInputStream fileIn = new FileInputStream(file);
			byte fileContent[] = new byte[(int)file.length()];
			fileIn.read(fileContent);
			user.setImage(fileContent);
			
			userDao.save(user);
			i++;
		}
		users.clear();
		users = null;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}
	
	public void init() throws IOException {
		loadData();
	}
}
